<?php
/**
 * Created by PhpStorm.
 * User: zengyuming
 * Date: 2017/8/16
 * Time: 上午10:18
 */

namespace Admin\Controller;
class NotifyController extends CommonController{

    private function RepeatCallback($transactionData){
        $update_orderstatus_tow = M('Transaction')->where(array('pay_status' => 1 , 'out_trade_no' => $transactionData['out_trade_no']))->find(); // 防止第二次异步通知时查询
        // 异步通知
        if ($update_orderstatus_tow['pay_status'] == 1) {
            $nonce_str = guid(); // 生成无重复随机字符串
            // 加入签名的参数
            $NotifySignData = array(
                // 异步通知参与的Sign参数
                'partner' => $transactionData['merchant'],
                'ordernumber' => $transactionData['out_trade_no'], // 用户支付时传入的订单号
                'orderstatus' => '1',
                'bank_type' => $transactionData['pay_type'],
                // 新加
                'passageway_id' => $transactionData['mch_id'], // 渠道商户号
                'passageway_type' => $transactionData['mch_type'], // 渠道名
                // 新加 The End
                'nonce_str' => $nonce_str,
                'paymoney' => $transactionData['paymoney']
            );

            $NotifySign = SetMySign($NotifySignData, $transactionData['merchant_key']);
            // 发送异步回调通知的参数
            $Send_NotifyData = array(
                'partner' => $transactionData['merchant'],
                'ordernumber' => $transactionData['out_trade_no'], // 用户支付时传入的订单号
                'orderstatus' => '1',
                'paymoney' => $transactionData['paymoney'],
                'bank_type' => $transactionData['pay_type'],
                'sysnumber' => $transactionData['systrade_no'], // 系统订单号
                // 新加
                'passageway_id' => $transactionData['mch_id'], // 渠道商户号
                'passageway_type' => $transactionData['mch_type'], // 渠道名
                // 新加 The End
                'nonce_str' => $nonce_str,
                'sign' => $NotifySign
            );

            $SendNotifyResult = https_request($transactionData['notify_url'], $Send_NotifyData);
            $file_name = './PayStatusLog/' . $Send_NotifyData['ordernumber'] . '.txt';
            if (!file_exists($file_name)) {
                file_put_contents($file_name, $SendNotifyResult);
            }
            if ($SendNotifyResult != "SUCCESS") {
                return false;
            } else {
                return true;
            }
        }
    }

    public function ReplayPayCallback($order_number ,$myKey1){
        $myKey = '95444015d88928b07abd8d6f93f0cb5b';
        if($myKey1 != $myKey){
            exit("key error");
        }
        // select * from zym_transaction where update_begtime > 1502794800 and update_begtime < 1502797200;
// select a.* , b.merchant_key from zym_transaction as a left JOIN zym_merchant as b on a.merchant = b.merchant_id where update_begtime > 1502794800 and update_begtime < 1502797200;
        $where = array(
//            'update_begtime' => array(array("EGT" ,  1502797212), array('ELT' , 1502840412)),
            'out_trade_no' => $order_number,
            'pay_status' => 1,
        );
//        $orderList = M("transaction")->where(array('out_trade_no' => 'lianhuan20170815190004196757'))->select();
        $orderList = M("transaction")->alias('a')->join('left JOIN zym_merchant as b on a.merchant = b.merchant_id')->where($where)->select();
        if(M("transaction")->where(array('out_trade_no' => $order_number))->count() <= 0){
            return 1;
        }
        // dump($orderList);die;
        foreach ($orderList as $value){
            if($this->RepeatCallback($value)) {
                return 3; // 成功

            }else{
                return 2;
            }
        }
    }


    public function shoudongCallback(){
        // 这是手动回调的方法
        $where = array(
            'update_begtime' => array(array("EGT" ,  1502797212), array('ELT' , 1502840412)),
            'pay_status' => 1,
        );
//        $orderList = M("transaction")->where(array('out_trade_no' => 'lianhuan20170815190004196757'))->select();
        $orderList = M("transaction")->alias('a')->join('left JOIN zym_merchant as b on a.merchant = b.merchant_id')->where($where)->select();
        // dump($orderList);die;
        foreach ($orderList as $value){
            if($this->RepeatCallback($value)) {
                echo 'SUCCESS' . '<br/>'; // 成功

            }else{
                echo 'fail' . '<br/>';
            }
        }
    }
}